package com.sfac.javaSpringBoot.modules.common.controller;

import com.sfac.javaSpringBoot.modules.common.entity.ImageType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//DictionaryController，字典类控制器，返回指定的枚举数据

@RestController
@RequestMapping("/api/dictionary")
public class DictionaryController {

    public static Map<String, Class> dictionaryMap = new HashMap<String, Class>() {{
        put("imageType", ImageType.class);
    }};
//    static {
//        dictionaryMap.put("imageType", ImageType.class);
//    }

    /**
     * 127.0.0.1/api/dictionary/imageType ---- get
     */
    @GetMapping("/{dictionaryName}")
    public List<Object> getDictionary(@PathVariable String dictionaryName) {
        try {
            Class clazz = dictionaryMap.get(dictionaryName);
            Method method = clazz.getMethod("values");
            Object[] results = (Object[]) method.invoke(null);
            return Arrays.asList(results);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}